package com.rd.sys.common.utils.report;

import com.alibaba.fastjson.JSON;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.MiniTableRenderData;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.data.style.TableStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PoiExportUtil {

  public static void exportEyeReport(String templatePath, String outPath, EyeCheckDataDto eyeData) throws Exception {
    Map<String, Object> dataMap = JSON.parseObject(JSON.toJSONString(eyeData), Map.class);
    ArrayList<RowRenderData> rowList = new ArrayList<RowRenderData>();
    if(eyeData.getTableHeader() != null && !eyeData.getTableHeader().isEmpty()){
      rowList.add(PoiExportUtil.POIDefaultTableStyle(eyeData.getTableHeader()));
    }
    if (eyeData.getTableData()!= null && !eyeData.getTableData().isEmpty()){
      for (int i = 0; i < eyeData.getTableData().size(); i++) {
        rowList.add(PoiExportUtil.POIDefaultTableStyle(eyeData.getTableData().get(i)));
      }
    }
    dataMap.put("tables", new MiniTableRenderData(rowList));
    PoiExportUtil.exportWord(templatePath, outPath, dataMap);
  }

  public static void exportWord(String templatePath, String outPath, Map<String, Object> dataMap) throws Exception {
    XWPFTemplate template = XWPFTemplate.compile(templatePath).render(dataMap);
    FileOutputStream out = new FileOutputStream(outPath);
    template.write(out);
    out.flush();
    out.close();
    template.close();
  }

  /**
   * 文本样式：宋体 黑色 16
   * 表格样式：黑色 居中
   * */
  public static RowRenderData POIDefaultTableStyle(List<String> rowTextList){
    RowRenderData rowRenderData = new RowRenderData();
    List<TextRenderData> rowData = new ArrayList<TextRenderData>();
    for (String text : rowTextList) {
      TextRenderData textRenderData = new TextRenderData(text);
      Style style = new Style("FFFFFF", 16);
      style.setFontFamily("宋体");
      textRenderData.setStyle(style);
      rowData.add(textRenderData);
    }
    TableStyle tableStyle = new TableStyle();
    tableStyle.setBackgroundColor("FFFFFF");
    tableStyle.setAlign(STJc.CENTER);
    rowRenderData.setRowData(rowData);
    rowRenderData.setStyle(tableStyle);
    return rowRenderData;
  }



  public static void main(String[] args) {
    EyeCheckDataDto eyeCheckDataDto = new EyeCheckDataDto();
    eyeCheckDataDto.setAge("34");
    eyeCheckDataDto.setChecker("张三");
    eyeCheckDataDto.setCheckTime("2020年11月20日");
    eyeCheckDataDto.setDept("五官科");
    eyeCheckDataDto.setMentalHistory("无");
    eyeCheckDataDto.setRemark("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
    eyeCheckDataDto.setName("病人某某某");
    eyeCheckDataDto.setReportTime("2020年11月25日");
    eyeCheckDataDto.setSex("男");
    List<String> tableHeader = new ArrayList<String>();
    tableHeader.add("AOI");
    tableHeader.add("指标");
    tableHeader.add("测试值");
    tableHeader.add("参考值");
    eyeCheckDataDto.setTableHeader(tableHeader);
    List<List<String>> tableData = new ArrayList<List<String>>();
    tableData.add(tableHeader);
    eyeCheckDataDto.setTableData(tableData);
    try {
      PoiExportUtil.exportEyeReport("D:\\\\1\\\\1.docx", "D:\\1\\out_template.doc", eyeCheckDataDto);
    }catch (Exception e){
      e.printStackTrace();
    }
  }

}
